package com.log.resolver;

import com.log.resolver.interfaces.LogDescribe;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DefaultLogDescribe
 * @Author xiezhuocai
 * @Description TODO
 * @Date 2022/5/12 22:45
 */
public class DefaultLogDescribe implements LogDescribe {

    private String stock;
    private String mSpEL;
    private String fun;

    public DefaultLogDescribe(String stock, String mSpEL, String fun){
        this.stock = stock;
        this.mSpEL = mSpEL;
        this.fun = fun;
    }

    @Override
    public Map<String,String> stockKey(){
        return analysis(stock);
    }

    @Override
    public Map<String,String> mSpELKey(){
        return analysis(mSpEL);
    }

    @Override
    public Map<String,String> funKey(){
        return analysis(fun);
    }

    private Map<String,String> analysis(String value){
        Map<String,String> result = new HashMap<>();
        if(StringUtils.isEmpty(value)){
            return result;
        }
        String[] splitStock = value.split(",");
        for (String splitValue:splitStock){
            String[] splitBody = splitValue.split("=");
            if(splitBody.length == 2){
                result.put(splitBody[0],splitBody[1]);
            }
        }
        return result;
    }
}
